/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.ucase.caseadditional.controller;

import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.dicenum.IsConfirmEnum;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.csource.manage.enums.CaseSourceEnum;
import com.icinfo.cloud.provider.punish.ucase.caseadditional.dto.CaseAdditionalRecordDto;
import com.icinfo.cloud.provider.punish.ucase.caseadditional.dto.CasePartiesPersonInfoAddDto;
import com.icinfo.cloud.provider.punish.ucase.caseadditional.enums.CaseAdditionalRecordCaseTypeEnums;
import com.icinfo.cloud.provider.punish.ucase.caseadditional.enums.CaseAdditionalRecordFlowEnums;
import com.icinfo.cloud.provider.punish.ucase.caseadditional.service.ICaseAdditionalRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CasePartiesLegalInfoDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CasePartiesPersonInfo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.mybatis.mapper.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 描述: 案件补录表 case_additional_record 对应的Controller类.<br>
 *
 * @author framework generator
 * @date 2022年12月08日
 */
@RestController
@RefreshScope
@RequestMapping("/punish/ucase/case/main/additional/record")
@Slf4j
@Component
public class CaseAdditionalRecordController extends BaseController {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseAdditionalRecordController.class);

    @Autowired
    private ICaseAdditionalRecordService caseAdditionalRecordService;
    @Autowired
    private ISysDepartParamService sysDepartParamService;
    /**
     * 描述：补录保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    @PostMapping("/saveOrUpdateById")
    @ApiOperation(value = "【V1.0.0】描述：补录保存", httpMethod = "POST")
    public Result<String> saveOrUpdateById(@RequestBody @Validated CaseAdditionalRecordDto recordDto) {
        if(StringUtil.isEmpty(recordDto.getOrgCode())){
            return  Result.error("登陆部门不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseStageStatus())){
            return  Result.error("补录阶段状态不为空");
        }
        if(CaseAdditionalRecordFlowEnums.A_1.getCode().equals(recordDto.getCaseStageStatus())){
            return  saveLa(recordDto);
        }else if(CaseAdditionalRecordFlowEnums.A_2.getCode().equals(recordDto.getCaseStageStatus())){
            return  saveGz(recordDto);
        }else if(CaseAdditionalRecordFlowEnums.A_3.getCode().equals(recordDto.getCaseStageStatus())){
            return  saveJd(recordDto);
        }else if(CaseAdditionalRecordFlowEnums.A_4.getCode().equals(recordDto.getCaseStageStatus())){
            return  saveZx(recordDto);
        }else if(CaseAdditionalRecordFlowEnums.A_5.getCode().equals(recordDto.getCaseStageStatus())){
            return  saveJa(recordDto);
        }else {
            return  saveSp(recordDto);
        }
    }

    /**
     * 描述：补录 立案保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveLa(CaseAdditionalRecordDto recordDto){
        if(recordDto.getPartiesRecordList().size()==0&&recordDto.getPartiesPersonInfoList().size()==0){
            return  Result.error("当事人信息或者个人信息不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseNumber())){
            return  Result.error("案件编号不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseName())){
            return  Result.error("处罚行为不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseSource())){
            return  Result.error("案件来源不为空");
        }
        if(CaseSourceEnum.CASE_SOURCE_YGBMYS.getDes().equals(recordDto.getCaseSource())){
            if(StringUtil.isEmpty(recordDto.getCaseOrgName())){
                return  Result.error("移送部门名称不为空");
            }
        }
        if(StringUtil.isEmpty(recordDto.getCaseCreateTimeStr())){
            return  Result.error("案件来源日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseHappenTimeStr())){
            return  Result.error("违法时间不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseRegistTimeStr())){
            return  Result.error("立案时间不为空");
        }
        if(recordDto.getMainRecordList().size()==0){
            return  Result.error("处罚事项不为空");
        }
        //当事人校验
        List<CasePartiesPersonInfoAddDto> partiesPersonInfoList = recordDto.getPartiesPersonInfoList();
        List<CasePartiesLegalInfoDto> partiesLegalInfoList = recordDto.getPartiesRecordList();
        if(!ObjectUtils.isEmpty(partiesPersonInfoList)){
            for (CasePartiesPersonInfo dto : partiesPersonInfoList) {
                if (ObjectUtils.isEmpty(dto.getAddressAreas())) {
                    return Result.error("住址-省市区不能为空（逗号分隔）");
                }
                if (ObjectUtils.isEmpty(dto.getAddress())) {
                    return Result.error("住址-详细地址不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getName())) {
                    return Result.error("当事人姓名不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getSex())) {
                    return Result.error("当事人性别不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getBirth())) {
                    return Result.error("当事人出生年月不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getCertType())) {
                    return Result.error("当事人证件类型不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getCertNo())) {
                    return Result.error("当事人证件号码不能为空");
                }
            }
        }
        if (!ObjectUtils.isEmpty(partiesLegalInfoList)) {
            for (CasePartiesLegalInfoDto dto : partiesLegalInfoList) {
                //当事人
                if (ObjectUtils.isEmpty(dto.getLegal())) {
                    return Result.error("法定代表人不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getUniscid())) {
                    return Result.error("统一社会信用代码不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getAddress())) {
                    return Result.error("单位的地址不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getPartyName())) {
                    return Result.error("单位名称不能为空");
                }
                if (ObjectUtils.isEmpty(dto.getEntType())) {
                    return Result.error("单位类型不能为空");
                }
            }
        }
        return caseAdditionalRecordService.saveLa(recordDto);
    }

    /**
     * 描述：补录 告知保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveGz(CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseKnowTimeStr())){
            return  Result.error("告知日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseKnowWay())){
            return  Result.error("告知方式不为空");
        }
        if(StringUtil.isEmpty(recordDto.getSendWay())){
            return  Result.error("送达方式不为空");
        }
        if(StringUtil.isEmpty(recordDto.getSentTimeStr())){
            return  Result.error("送达日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseKnowInform())){
            return  Result.error("权利告知不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsHearing())){
            return  Result.error("是否可听证不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCasePunishKinds())){
            return  Result.error("告知处罚种类不为空");
        }
        return caseAdditionalRecordService.saveGz(recordDto);
    }
    /**
     * 描述：补录 决定保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveJd(CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        if(StringUtil.isEmpty(recordDto.getDocumentCreateTimeStr())){
            return  Result.error("决定日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getDocumentNumber())){
            return  Result.error("决定书文号不为空");
        }
        if(StringUtil.isEmpty(recordDto.getDecisionSentTimeStr())){
            return  Result.error("决定送达日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getDecisionSentWay())){
            return  Result.error("决定送达方式不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseDocumentCategory())){
            return  Result.error("决定类别不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsImportantCase())){
            return  Result.error("是否重大案件不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsDocumentAudit())){
            return  Result.error("是否法制审核不为空");
        }
        if(YesOrNoEnum.YES.getCode().equals(recordDto.getIsDocumentAudit()) && recordDto.getDocumentAuditDate() == null){
            return  Result.error("法制审核日期不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsNeedCollectiveDiscuss())){
            return  Result.error("是否集体讨论不为空");
        }
        if(IsConfirmEnum.IsConfirmXY.getCode().equals(recordDto.getIsNeedCollectiveDiscuss())){
            if(StringUtil.isEmpty(recordDto.getNeedCollectiveDiscussTimeStr())){
                return  Result.error("集体讨论日期不为空");
            }
            if(StringUtil.isEmpty(recordDto.getNeedCollectiveDiscussReason())){
                return  Result.error("集体讨论结论不为空");
            }
        }
        if(StringUtil.isEmpty(recordDto.getIllegalFact())){
            return  Result.error("违法事实不为空");
        }
        if(StringUtil.isEmpty(recordDto.getRelateEvidence())){
            return  Result.error("违法依据不为空");
        }
        if(StringUtil.isEmpty(recordDto.getPunishBasis())){
            return  Result.error("处罚依据不为空");
        }
        if(StringUtil.isEmpty(recordDto.getPunishKinds())){
            return  Result.error("处罚种类不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsPublicity())){
            return  Result.error("是否推送公示信息不为空");
        }
        if(StringUtil.isEmpty(recordDto.getPublicityContent())){
            return  Result.error("公示文本内容不为空");
        }
//        if(recordDto.getAppendixRecordList().size()==0){
//            return  Result.error("上传附件不为空");
//        }
        return caseAdditionalRecordService.saveJd(recordDto);
    }
    /**
     * 描述：补录 执行保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveZx(CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseCloseStatus())){
            return  Result.error("结案状态不能为空");
        }
        //判断是否结案，如果是已结案，正常走流程
        if(!YesOrNoEnum.isLegalEnumCode(recordDto.getCaseCloseStatus())){
            return  Result.error("结案状态值非法，应为0或者1");
        }
        //判断是否结案，根据不同选项，执行不同逻辑
        if(YesOrNoEnum.NO.getCode().equals(recordDto.getCaseCloseStatus())){
            return caseAdditionalRecordService.saveZxCaseNotClose(recordDto);
        }else {
            if(StringUtil.isEmpty(recordDto.getIsExecutePunish())){
                return  Result.error("是否执行处罚决定不为空");
            }
            if(CaseAdditionalRecordCaseTypeEnums.A_1.getCode().equals(recordDto.getIsExecutePunish())){
                if(StringUtil.isEmpty(recordDto.getExecuteWay())){
                    return  Result.error("执行方式不为空");
                }
                if(StringUtil.isEmpty(recordDto.getExecuteFinishDateStr())){
                    return  Result.error("执行完成日期不为空");
                }
                if(StringUtil.isEmpty(recordDto.getIsStages())){
                    return  Result.error("是否分期不为空");
                }
                if(StringUtil.isEmpty(recordDto.getIsDelay())){
                    return  Result.error("是否延期不为空");
                }
            }else {
                if(StringUtil.isEmpty(recordDto.getExecutePunishCause())){
                    return  Result.error("未执行处罚决定原因不为空");
                }
            }
            if(StringUtil.isEmpty(recordDto.getCasePunishResult())){
                return  Result.error("处罚结果不为空");
            }
            return caseAdditionalRecordService.saveZx(recordDto);
        }
    }
    /**
     * 描述：补录 结案保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveJa(CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsCaseCondition())){
            return  Result.error("结案情况-不予行政处罚不为空");
        }
        if(StringUtil.isEmpty(recordDto.getIsCasePunish())){
            return  Result.error("是否涉刑案件不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseFinishDateStr())){
            return  Result.error("结案日期不为空");
        }
        return caseAdditionalRecordService.saveJa(recordDto);
    }
    /**
     * 描述：补录 审批表信息保存
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    private Result<String> saveSp(CaseAdditionalRecordDto recordDto){

        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        return caseAdditionalRecordService.saveSp(recordDto);
    }
    /**
     * 【V1.6.0】描述：补录列表
     *
     * @param recordDto 入参
     * @return List<CaseAdditionalRecordDto>
     * @author 王发魏
     * @date 2022-12-09
     **/
    @RequestMapping("/getCaseAdditionalRecordList")
    @ApiOperation(value = "【V1.0.0】描述：补录列表", httpMethod = "POST")
    public Result<PageResponse<CaseAdditionalRecordDto>> getCaseAdditionalRecordList(@RequestBody @Validated CaseAdditionalRecordDto recordDto) {
        System.out.println(System.currentTimeMillis());
        if(recordDto.getPageNum() == null){
            return Result.error("分页-每页页数不为空");
        }
        if(recordDto.getLength() == null){
            return Result.error("分页-每页大小不为空");
        }
        List<CaseAdditionalRecordDto> list = caseAdditionalRecordService.getCaseAdditionalRecordList(recordDto);
        return Result.success(Constant.STRING_GET_SUCCESS, new PageResponse<>(list));
    }
    /**
     * 描述：补录详情
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    @PostMapping("/getRecord")
    @ApiOperation(value = "【V1.0.0】描述：补录详情", httpMethod = "POST")
    public Result<CaseAdditionalRecordDto> getRecord(@RequestBody @Validated CaseAdditionalRecordDto recordDto) {
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        if(StringUtil.isEmpty(recordDto.getCaseStageStatus())){
            return  Result.error("补录阶段状态不为空");
        }
        return caseAdditionalRecordService.getAdditionalRecord(recordDto);
    }
    /**
     * 描述：根据部门编码查询部门信息
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    @PostMapping("/getSysDepartParam")
    @ApiOperation(value = "【V1.0.0】描述：根据部门编码查询部门信息", httpMethod = "POST")
    public Result<SysDepartParam> getSysDepartParam(@RequestBody @Validated CaseAdditionalRecordDto recordDto) {
        if(StringUtil.isEmpty(recordDto.getOrgCode())){
            return  Result.error("登陆部门编码不为空");
        }
        return sysDepartParamService.getSysDepartParam(recordDto);
    }
    /**
     * 描述：补录-详情
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    @PostMapping("/detail")
    @ApiOperation(value = "【V1.0.0】描述：补录详情", httpMethod = "POST")
    public Result<CaseAdditionalRecordDto> detail(@RequestBody @Validated CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        return caseAdditionalRecordService.detail(recordDto);
    }
    /**
     * 描述：补录-删除
     * @param recordDto
     * @return
     * @author wangfawei
     * @date 2022-12-8
     */
    @PostMapping("/deleteRecord")
    @ApiOperation(value = "【V1.0.0】描述：补录删除", httpMethod = "POST")
    public Result<String> deleteRecord(@RequestBody @Validated CaseAdditionalRecordDto recordDto){
        if(StringUtil.isEmpty(recordDto.getId())){
            return  Result.error("主键id不为空");
        }
        return caseAdditionalRecordService.deleteRecord(recordDto);
    }
}